package com.jsonpfy.webservices;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsonpfy.operations.SaveData;

@SuppressWarnings("serial")
public class SaveDataServlet extends HttpServlet {

	// http://localhost:8888/SaveDataService?
	// kind=Comment
	// &fieldsKind=String&fieldsName=description&fieldsValue=Test 2
	// &fieldsKind=String&fieldsName=projectID&fieldsValue=Projectnection
	// &fieldsKind=String&fieldsName=classPath&fieldsValue=com.projectnection.client.Entities.EntityPage
	// &fieldsKind=String&fieldsName=username&fieldsValue=Alline Watkins
	// &fieldsKind=String&fieldsName=email&fieldsValue=alline.oliveira@gmail.com
	// &fieldsKind=String&fieldsName=facebookUserID&fieldsValue=alline.oliveira
	// &fieldsKind=Date&fieldsName=date&fieldsValue=1313085767484

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		String kind = request.getParameter("kind");

		String ID = request.getParameter("ID");

		String[] fieldsKind = request.getParameterValues("fieldsKind");

		String[] fieldsName = request.getParameterValues("fieldsName");

		String[] fieldsValue = request.getParameterValues("fieldsValue");

		SaveData.doPut(kind, ID, fieldsKind, fieldsName, fieldsValue);

		String answer = Utilities.getCallback(request.getParameter("callback"),
				"OK");

		response.getWriter().println(answer);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		String kind = request.getParameter("kind");

		String ID = request.getParameter("ID");

		String[] fieldsKind = request.getParameterValues("fieldsKind");

		String[] fieldsName = request.getParameterValues("fieldsName");

		String[] fieldsValue = request.getParameterValues("fieldsValue");

		SaveData.doPut(kind, ID, fieldsKind, fieldsName, fieldsValue);

		String answer = Utilities.getCallback(request.getParameter("callback"),
				"OK");

		response.getWriter().println(answer);
	}
}
